package uk.gov.swmprobation.cplitebase;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import uk.gov.swmprobation.cplitebase.R;

import android.annotation.TargetApi;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ResolveInfo;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;
import android.view.Display;
import android.view.MenuItem;

import com.google.analytics.tracking.android.EasyTracker;

public class MyActivity extends ActionBarActivity implements ActionBar.TabListener {
	
	public static SharedPreferences sharedPref;
    public ActionBar actionBar;
    public EasyTracker easyTracker;
    Long startdate;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
		sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
		
		if (sharedPref.getInt("Max_Picture_Size", 0)==0){
			setPictureSizeMax();
		}
				
		easyTracker = EasyTracker.getInstance(this);
		
        actionBar = getSupportActionBar();    
	}
	
	@Override
	protected void onStart(){
		super.onStart();
		if (sharedPref.getBoolean("allowanalytics", true)){
			EasyTracker.getInstance(this).activityStart(this);
		}
	}
	
	@Override
	protected void onStop(){
		super.onStop();
		EasyTracker.getInstance(this).activityStop(this);
	}
	
	@Override
	protected void onResume(){
		super.onResume();
		startdate = new Date().getTime();
	}
	
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
    	int itemID =item.getItemId();
        if (itemID == R.id.menu_settings) {
			if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB){
				startActivity(new Intent(this, SettingsActivity.class));
			} else {
				startActivity(new Intent(this, SettingsFragment.class));
			}
			return true;
		} else if (itemID == R.id.menu_about) {
			startActivity(new Intent(this,About.class));
			return true;
		} else if (itemID == R.id.menu_privacy) {
			startActivity(new Intent(this,Privacy.class));
			return true;
		}else {
			return super.onOptionsItemSelected(item);
		}
    }
    
    
	@SuppressWarnings("deprecation")
	@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
	private void setPictureSizeMax(){
		int width=0,height=0,max_size;
		Display display = getWindowManager().getDefaultDisplay();
		if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2){
			width = display.getWidth();
			height = display.getHeight();
		} else {
			Point size = new Point();
			display.getSize(size);
			width = size.x;
			height = size.y;
		}
		max_size = Math.round((Math.max(width, height)/4));
		
		sharedPref.edit().putInt("Max_Picture_Size", max_size).commit();
    }
    
	@Override
	public void onTabReselected(Tab tab,
			android.support.v4.app.FragmentTransaction arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTabSelected(Tab tab,
			android.support.v4.app.FragmentTransaction arg1) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onTabUnselected(Tab tab,
			android.support.v4.app.FragmentTransaction arg1) {
		// TODO Auto-generated method stub
		
	}
	
	protected long getDuration(){
		return new Date().getTime() - startdate;
	}
	
	static Intent shareLink(Project project){
		List<Intent> targetedShareIntents = new ArrayList<Intent>();

		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.setType("text/plain");

		List<ResolveInfo> resInfo = MyApplication.getContext().getPackageManager().queryIntentActivities(intent, 0);

		for (ResolveInfo resolveInfo : resInfo) {
		   String packageName = resolveInfo.activityInfo.packageName;
		   
		   Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
		   targetedShareIntent.setType("text/plain");
		   targetedShareIntent.putExtra(Intent.EXTRA_TEXT,"I want to share this with you!");
		   targetedShareIntent.putExtra(Intent.EXTRA_SUBJECT, "I've suuggested a Community Payback project");
		   targetedShareIntent.setPackage(packageName);

		   
		   if (packageName.equals("com.facebook.katana")) { // Remove Facebook Intent share
			      
		   } else {
		      targetedShareIntents.add(targetedShareIntent);
		   }
		  }
		/*
		// Add my own activity in the share Intent chooser
		Intent i = new Intent(this, NextActivity.class);
		targetedShareIntents.add(i);
		*/

		Intent chooserIntent = Intent.createChooser(
		   targetedShareIntents.remove(0), "Share with...");

		chooserIntent.putExtra(
		   Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));

		return chooserIntent;
	}
	
}
